const cloud = require('wx-server-sdk')
const db = cloud.database()

exports.main = async (event,context)=>{
	if(event.method == 'post'){
		await db.collection('base_info').add({
			data:event.data
		});
		return {'msg':'ok','data':result};
	}else{
		let result = await db.collection('base_info').get();
		let newData = [];
		let userid = ''
		for (item of result.data){
			if(item.type == 'info'){
				let conss = null;
				try{
					conss = Buffer.from(item['biography'], 'base64').toString().replace(/\u/g, "%u").replace(/\\/g,"");
				}catch{
					conss = item['biography']
				}
				item['biography'] = conss;
				newData.push(item)
			}
			if(item.username == event.user){
				userid = item.id
			}
			if(item.type == 'reels' && item.id == userid){
				newData.push(item)
			}
			if(item.type == 'music'){
				newData.push(item)
			  }
		}
		return {'msg':'ok','data':newData};
	}
}